package io.sdl.controller;

import io.sdl.common.sys.entity.SdlButtonClickStatsEntity;
import io.sdl.common.sys.entity.SdlMessageReadStatusEntity;
import io.sdl.common.utils.Result;
import io.sdl.common.sys.service.SdlButtonClickStatsService;
import io.sdl.dto.ClickCountDto;
import io.sdl.entity.TokenEntity;
import io.sdl.service.TokenService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;


/**
 * 按钮点击统计汇总表
 *
 * @author Dchen
 * @email 1211734836@qq.com
 * @date 2025-10-22 09:24:31
 */
@RestController
@RequestMapping("/api")
public class SdlButtonClickStatsController {
	@Autowired
	private SdlButtonClickStatsService sdlButtonClickStatsService;

	@Autowired
	private TokenService tokenService;


	@PostMapping("recordButtonClick")
	@Operation(summary = "点击次数保存")
	public Result recordButtonClick(@RequestBody ClickCountDto dto) {

		if (!dto.getToken().isEmpty()) {
			TokenEntity byToken = tokenService.getByToken(dto.getToken());
		}

		//获取按钮操作信息
		SdlButtonClickStatsEntity countByButtonName = sdlButtonClickStatsService.getCountByButtonName(dto.getButtonName(), dto.getPageName());

		SdlButtonClickStatsEntity sdlButtonClickStatsEntity = new SdlButtonClickStatsEntity();
		Integer clickCount = 0;


		if (countByButtonName != null) {
			clickCount = countByButtonName.getClickCount();
			countByButtonName.setClickCount(clickCount + 1);
			countByButtonName.setStatDate(new Date());
			sdlButtonClickStatsService.updateById(countByButtonName);
		} else {
			sdlButtonClickStatsEntity.setClickCount(1);
			sdlButtonClickStatsEntity.setButtonName(dto.getButtonName());
			sdlButtonClickStatsEntity.setPageName(dto.getPageName());
			sdlButtonClickStatsEntity.setStatDate(new Date());
			sdlButtonClickStatsService.insert(sdlButtonClickStatsEntity);
		}


		return new Result();
	}

}
